﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.ShipApplyCommand;
using WarehouseManagement.Domain.DTO.ShipApply;
using WarehouseManagement.Infrstructre.Interface;
using static ERPPublic.ParseAPI;
namespace WarehouseManagement.Application.CommandHandler.ShipApplyCommandHandlers
{
    public class FillShipCommandHandlers : IRequestHandler<FillShipCommand, FillShipDTO>
    {

        private readonly IShipRequestRepository _ship;
        private readonly IShipRequestProRepository _shipPro;


        public FillShipCommandHandlers(IShipRequestRepository ship, IShipRequestProRepository shipPro)
        {
            _ship = ship;
            _shipPro = shipPro;
        }

        public async Task<FillShipDTO> Handle(FillShipCommand request, CancellationToken cancellationToken)
        {
            var ship = (from a in (await _ship.GetAll()) select a).ToList();
            var shipPro = (from a in (await _shipPro.GetAll()) select a).ToList();
           
            //List<Sale> saleContrace = await GetSaleList(1, 100);
            //List<Return> Returns = await GetREturnList(1, 100);
            //List<Products> Product = await GetProductList("","",1, 100);
            var dto = (from a in ship
                       where a.ShippRequestAppId == request.ShipId
                       select new FillShipDTO
                       {
                           ShippRequestAppId = a.ShippRequestAppId,
                           SRANumber = a.SRANumber,
                           Name = a.Name,
                           ClientId = a.ClientId,
                           ManpowerId = a.ManpowerId,
                           ReturnProductId = a.ReturnProductId,
                           SaleContractProductId = a.SaleContractProductId,
                           ShipType = a.ShipType,
                           SPRTopic = a.SPRTopic,
                           SRAApplicant = a.SRAApplicant,
                           SRAConsigner = a.SRAConsigner,
                           SRACourierCharges = a.SRACourierCharges,
                           SRADeliveryDate = a.SRADeliveryDate,
                           SRADueDate = a.SRADueDate,
                           SRAReceivAddress = a.SRAReceivAddress,
                           SRAReceivePhone = a.SRAReceivePhone,
                           SRAReceivPeople = a.SRAReceivPeople,
                           SRAShipmentsType = a.SRAShipmentsType,
                           SRATrackingNumber = a.SRATrackingNumber,
                           SRARemark = a.SRARemark,
                           VendorId = a.VendorId,
                           SRACourierCompanie = a.SRACourierCompanie,
                           AddName = a.AddName,
                           AddTime = a.AddTime,
                           Office_Id = a.Office_Id,
                           User_Id = a.User_Id,
                           detail = (from b in shipPro 
                                    // join pro  in Product on b.ProductId equals pro.ProductId
                                     where a.SRANumber == b.SRANumber
                                     select new FillShipApplyDetailDTO
                                     {
                                         ShipProductAppId = b.ShipProductAppId,
                                         SRANumber = b.SRANumber,
                                         PackMethod = b.PackMethod,
                                         ProductId = b.ProductId,
                                         Remark = b.Remark,
                                         PName = b.PName,
                                         PSpec = b.PSpec,
                                          PUnit = b.PUnit,
                                           PNumber = b.PNumber,
                                           ProNum = b.ProNum
                                           
                                     }).ToList(),

                       }).First();
            return dto;
        }


        /// <summary>
        /// 获取采购退货列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetREturnList(int pageIndex,int pageSize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string salecontrace = "http://116.205.121.197:8000/cg/api/PurchaseRefund/GetProcurementReturnList?Pindex="+pageIndex+"&Psize="+pageSize;
            string postReponse = await cilent.GetAsync(salecontrace);
            var json = JsonConvert.DeserializeObject<ApiResultList<Return>>(postReponse);
            return json.data.list;
        }

        /// <summary>
        /// 获取销售合同列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetSaleList(int pageIndex,int pageSize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string salecontrace = "http://116.205.121.197:8000/cw/api/Sale/ShowStateSalesReturn";
            string postReponse = await cilent.GetAsync(salecontrace);
            var json = JsonConvert.DeserializeObject<ApiResultList<Sale>>(postReponse);
            return json.data.list;
        }

        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }


        /// <summary>
        /// 销售合同
        /// </summary>
        public class Sale
        {
            /// <summary>
            /// 销售合同id
            /// </summary>
            [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] // 主键和自增
            public int SaleContractId { get; set; }

            /// <summary>
            /// 客户id
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 客户名称
            /// </summary>
            public string? ClientName { get; set; }
            /// <summary>
            /// 签约日期
            /// </summary>
            public DateTime? SCAgencyDate { get; set; }
            /// <summary>
            /// 合同主题
            /// </summary>
            public string? ContractTopic { get; set; }
            /// <summary>
            /// 合同编号
            /// </summary>
            public string? SCcontractNumber { get; set; }
            /// <summary>
            /// 收货人
            /// </summary>
            public string? SCReceivPeople { get; set; }
            /// <summary>
            /// 手机号码
            /// </summary>
            public string? SCRePhone { get; set; }
            /// <summary>
            /// 详细地址
            /// </summary>
            public string? SCAddress { get; set; }
            /// <summary>
            /// 数量合计
            /// </summary>
            public int? SCNum { get; set; }
            
            public int? ProductId { get; set; }

        }

        /// <summary>
        /// 采购退货
        /// </summary>
        public class Return
        {
            /// <summary>
            /// 采购退货id
            /// </summary>
            [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] // 主键和自增
            public int PurchaseReturnsIds { get; set; }
            /// <summary>
            /// 采购退货编号
            /// </summary>
            public string? PurchaseReturnsCode { get; set; }
            /// <summary>
            /// 采购退货主题
            /// </summary>
            public string? PurchaseReturnsTopic { get; set; }
            /// <summary>
            /// 产品id
            /// </summary>
            public int? ProductId { get; set; }
            /// <summary>
            /// 供应商名称
            /// </summary>
            public string? VName { get; set; }
            /// <summary>
            /// 采购人员
            /// </summary>
            public string? PPurchasingStaff { get; set; }
            /// <summary>
            /// 退货人员
            /// </summary>
            public string? PReturnsPersonnel { get; set; }
            /// <summary>
            /// 单据编号
            /// </summary>
            public string? PRDocumentNumber { get; set; }
            /// <summary>
            /// 我方代表
            /// </summary>
            public string? PRPeople { get; set; }
            /// <summary>
            /// 对方代表
            /// </summary>
            public string? PRCountpartPeople { get; set; }
            /// <summary>
            /// 退货类别
            /// </summary>
            public string? PRReturnType { get; set; }
            /// <summary>
            /// 退货日期
            /// </summary>
            public DateTime? PRReturnDate { get; set; }
            /// <summary>
            /// 退款方式
            /// </summary>
            public string? PRReturnManner { get; set; }
            /// <summary>
            /// 退货地址
            /// </summary>
            public string? PRReturnAddress { get; set; }
            /// <summary>
            /// 数量合计
            /// </summary>
            public int? PRTotalQuantity { get; set; }
            /// <summary>
            /// 已退货金额
            /// </summary>
            public decimal? PRtotalAmount { get; set; }
        }

    }
}
